//
//  SelectImageView.m
//  ZLSQ_owner
//
//  Created by ihope99 on 2017/3/17.
//  Copyright © 2017年 iHope. All rights reserved.
//

#import "SelectImageView.h"
#import "ZXImageView.h"

@implementation SelectImageView


-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        mScrollView = [[UIScrollView alloc]initWithFrame:self.bounds];
        [self addSubview:mScrollView];
        
        self.photoManager = [[PhotoSelectManager alloc] init];
        self.photoManager.mbEdit = NO;
        self.photoManager.miWidth = 600;
        self.photoManager.miHeight = 800;
        WS(weakSelf);
        self.photoManager.PhotoFinishBlock = ^(NSArray *array) {
            weakSelf.nowImageArray = [NSMutableArray arrayWithArray:array];
            [weakSelf reloadView:weakSelf.nowImageArray];
            if (weakSelf.changeImageBlock) {
                weakSelf.changeImageBlock(weakSelf.nowImageArray);
            }
        };
    }
    return self;
}
-(void)reloadView:(NSArray *)mArray{
    
    for (UIView *mView in mScrollView.subviews) {
        [mView removeFromSuperview];
    }
    
    _nowImageArray = [NSMutableArray arrayWithArray:mArray];
    
    NSInteger count = mArray.count >= self.maxIndex ? self.maxIndex:(mArray.count +1);
    
    
    NSInteger Width = mScrollView.frame.size.height - 20;
    
    mScrollView.contentSize = CGSizeMake( count*(Width+10) + 12,mScrollView.frame.size.height);
    
    for (NSInteger i = 0; i < count; i ++ ) {
        if (i < mArray.count) {
            ZXImageView *mImageView = [[ZXImageView alloc]initWithFrame:CGRectMake(12 + (Width + 10)*i,10, Width, Width)];
            [mImageView GetImagePath:mArray[i]];
            [mScrollView addSubview:mImageView];
            
            UIButton *mDelectButton = [UIButton buttonWithType:UIButtonTypeCustom ];
            mDelectButton.frame =CGRectMake(CGRectGetMaxX(mImageView.frame) - 15, CGRectGetMinY(mImageView.frame) - 10, 20, 20);
            [mDelectButton setImage:[UIImage imageNamed:@"bt_dynamic_del_focused"] forState:UIControlStateNormal];
            mDelectButton.tag = 2000 + i;
            [mDelectButton addTarget:self action:@selector(delegateTouch:) forControlEvents:UIControlEventTouchUpInside];
            [mScrollView addSubview:mDelectButton];
        }
        else{
            UIImageView *mImageView = [[UIImageView alloc]initWithFrame:CGRectMake(12 + (Width + 10)*i,10, Width, Width)];
            mImageView.image = [UIImage imageNamed:@"image_add"];
            [mScrollView addSubview:mImageView];
            mImageView.userInteractionEnabled = YES;
            
            UITapGestureRecognizer *mTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(iamgeSelect:)];
            [mImageView addGestureRecognizer:mTap];
        }
    }
    
}

-(void)iamgeSelect:(UITapGestureRecognizer *)tap{
    WS(weakSelf);
    [MTAlert showSheetWithTitle:nil message:nil cancel:@"取消" ok:@[@"拍照",@"相册"] clickBlock:^(NSInteger tag) {
        if (tag != 0) {
            if (tag == 1) {
                [weakSelf.photoManager takePhoto];
            }
            else if (tag == 2){
                [weakSelf.photoManager TakeAlbum];
            }
        }
    }];
}
-(void)delegateTouch:(UIButton *)button{
    NSInteger count = button.tag - 2000;
    [self.photoManager delectImageWithIndex:count];

}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
